package com.chcit.mobile.mvp.presenter;

import android.app.Application;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.chcit.mobile.R;
import com.chcit.mobile.app.contains.HttpMethodContains;
import com.chcit.mobile.mvp.contract.HomeContract;
import com.chcit.mobile.mvp.model.entity.ChildrenBean;
import com.chcit.mobile.mvp.model.entity.HomeItem;
import com.chcit.mobile.mvp.model.entity.MenuBean;
import com.chcit.mobile.mvp.model.entity.ResultBean;
import com.chcit.mobile.mvp.model.entity.User;
import com.chcit.mobile.mvp.ui.activity.LoginActivity;
import com.chcit.mobile.mvp.ui.adapter.HomeAdapter;
import com.chcit.mobile.mvp.ui.fragment.InventoryFragment;
import com.chcit.mobile.mvp.ui.fragment.InventoryPlanFragment;
import com.chcit.mobile.mvp.ui.fragment.InventoryPlanQueryFragment;
import com.chcit.mobile.mvp.ui.fragment.MaintainFragment;
import com.chcit.mobile.mvp.ui.fragment.MovenFragment;
import com.chcit.mobile.mvp.ui.fragment.ReceiveFragment;
import com.chcit.mobile.mvp.ui.fragment.ShipmentFragment;
import com.elvishew.xlog.XLog;
import com.jess.arms.base.app.ConfigKeys;
import com.jess.arms.base.app.Quick;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;


@ActivityScope
public class HomePresenter extends BasePresenter<HomeContract.Model, HomeContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    @Inject
    List<HomeItem> mHomeItems;

    @Inject
    HomeAdapter mAdapter;

    @Inject
    public HomePresenter(HomeContract.Model model, HomeContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
        mHomeItems = null;
        mAdapter = null;

    }

    public void getMenues(){
        mModel.getMenues().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
               /* .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                })
                .doFinally(() -> {
                    mRootView.hideLoading();
                })*/
                .subscribe(new ErrorHandleSubscriber<List<ChildrenBean>>(mErrorHandler){

                    @Override
                    public void onNext( List<ChildrenBean> menuBeans) {
                        for (int i = 0; i < menuBeans.size(); i++) {
                            ChildrenBean childrenBean = menuBeans.get(i);
                            HomeItem item = new HomeItem();
                            item.setTitle(childrenBean.getText());
                            String ItemClass = childrenBean.getUrl();
                            Class<?> delegate = ReceiveFragment.class;
                            int imageResource;
                            if ("Mobile_Pickup2".equals(ItemClass)) {//拣货
                                delegate = ShipmentFragment.class;
                                imageResource = R.mipmap.purchasereceive;
                            } else if ("Mobile_Putaway".equals(ItemClass)) {//上架
                                delegate = MaintainFragment.class;
                                imageResource = R.mipmap.allotreceive;
                            } else if ("Mobile_Movement".equals(ItemClass)) {//移库
                                delegate = MovenFragment.class;
                                imageResource = R.mipmap.allotrejectreceive;
                            } else if ("Mobile_TaskToDo".equals(ItemClass)) {//待处理任务
                               // delegate = PRShipmentDelegate.class;
                                imageResource = R.mipmap.pr_shipment;
                            } else if ("Mobile_Receive".equals(ItemClass)) {//收货
                                delegate = ReceiveFragment.class;
                                imageResource = R.mipmap.mv_shipment;
                            } else if ("Mobile_Storage".equals(ItemClass)) {//库存查询
                                delegate = InventoryFragment.class;
                                imageResource = R.mipmap.shipmentquery;
                            }else if("Mobile_InventoryPlan".equals(ItemClass)){
                                delegate = InventoryPlanQueryFragment.class;
                                imageResource = R.mipmap.stockquery;
                            }else {
                                //delegate = LoginDelegate.class;
                                imageResource = R.mipmap.purchasereceive;
                            }
                            item.setActivity(delegate);
                            item.setImageResource(imageResource);
                            item.setUrl(Quick.getConfiguration(ConfigKeys.API_HOST)+HttpMethodContains.IMAGE_URL+childrenBean.getIcon());
                            mHomeItems.add(item);
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }
}
